<?php
class conexion{
	private $dbhost='localhost';
	private $dbusername='colegio7_mvu';
	private $dbuserpass='uuzxZRb@nQh6';
	private $dbname='colegio7_mv';
	private $mysqli;
	private $status;
	
	function __construct()
	{
		$this->status = "0x1";
		$this->openConexion();
	}
	
	private function openConexion(){
		$this->mysqli = new mysqli($this->dbhost, $this->dbusername, $this->dbuserpass, $this->dbname);
		if (mysqli_connect_errno($this->mysqli)) {
			$this->status = "0x1";
		}else{
			$this->status = "0x0";
		}
	}
	
	public function closeConexion(){
		$this->mysqli->close();
		$this->status = "0x1";
	}

	public function getStatus(){
		return $this->status;
	}
	
	public function getAllMovies(){
		$query = "SELECT m.*, mp.nameMpaa FROM movie m, mpaa mp WHERE m.idMpaa = mp.idMpaa ORDER BY releaseDate DESC";
		if($result = $this->mysqli->query($query)){
			$i=0;
			while ($row = $result->fetch_assoc()) {
				$buffer[$i] = $row;
				$buffer[$i]["title"] = base64_encode($buffer[$i]["title"]);
				$buffer[$i]["video"] = $this->getVideoMovie($row["idMovie"]);
				$buffer[$i]["audio"] = $this->getAudioMovie($row["idMovie"]);
				$buffer[$i]["genre"] = $this->getGenreMovie($row["idMovie"]);
				$i++;
			}
			if(isset($buffer)){
				return $buffer;
			}else{
				return '0x0';
			}
		}else{
			return '0x1';
		}
	}
	
	public function getVideoMovie($idMovie){
		$query = "SELECT vq.idVideo, vq.nameVideo FROM videoQuality vq, videoMovie vm WHERE vq.idVideo = vm.idVideo AND vm.idMovie = $idMovie";
		if($result = $this->mysqli->query($query)){
			$i=0;
			while ($row = $result->fetch_assoc()){
				$buffer[$i] = $row;
				$i++;
			}
			if(isset($buffer)){
				return $buffer;
			}else{
				return '0x0';
			}
		}else{
			return '0x1';
		}
	}
	
	public function getAudioMovie($idMovie){
		$query = "SELECT al.* FROM audioLanguage al, audioMovie am WHERE am.idAudio = al.idAudio AND am.idMovie = $idMovie";
		if($result = $this->mysqli->query($query)){
			$i=0;
			while ($row = $result->fetch_assoc()){
				$buffer[$i] = $row;
				$buffer[$i]["nameAudio"] = base64_encode($row["nameAudio"]);
				$i++;
			}
			if(isset($buffer)){
				return $buffer;
			}else{
				return '0x0';
			}
		}else{
			return '0x1';
		}
	}
	
	public function getGenreMovie($idMovie){
		$query = "SELECT g.idGenre, g.nameGenre FROM genre g, genreMovie gm WHERE gm.idGenre = g.idGenre AND gm.idMovie = $idMovie";
		if($result = $this->mysqli->query($query)){
			$i=0;
			while ($row = $result->fetch_assoc()){
				$buffer[$i] = $row;
				$buffer[$i]["nameGenre"] = base64_encode($row["nameGenre"]);
				$i++;
			}
			if(isset($buffer)){
				return $buffer;
			}else{
				return '0x0';
			}
		}else{
			return '0x1';
		}
	}

	public function setMovie($title, $originalTitle, $duration, $releaseDate, $idMpaa, $imdbRating, $imdbCode, $youtubeCode, $image, $sinopsis){
		$dateCreate = date("Y-m-d H:i:s");
		$query ="INSERT INTO movie (title, originalTitle, duration, releaseDate, idMpaa, imdbRating, imdbCode, youtubeCode, image, sinopsis, dateCreate)";
		$query .=" VALUES ('$title', '$originalTitle', $duration, '$releaseDate', $idMpaa, '$imdbRating', '$imdbCode', '$youtubeCode', '$image', '$sinopsis', '$dateCreate')";
		if($this->mysqli->query($query)){
			echo $this->mysqli->error;
			return $this->mysqli->insert_id;
		}else{
			return '0x1';
		}	
	}
	public function setVideoMovie($idMovie, $video){
		$query = "";
		foreach($video as $value){
			$query .= "INSERT INTO videoMovie (idMovie, idVideo) VALUES ($idMovie, $value); ";
		}
		if($this->mysqli->multi_query($query)){
			return '0x0';
		}else{
			echo $this->mysqli->error;
			return '0x1';
		}
	}
	public function setAudioMovie($idMovie, $audio){
		$query = "";
		foreach($audio as $value){
			$query .= "INSERT INTO audioMovie (idMovie, idAudio) VALUES ($idMovie, $value); ";
		}
		$this->closeConexion();
		$this->openConexion();
		if($this->mysqli->multi_query($query)){
			return '0x0';
		}else{
			echo $this->mysqli->error;
			return '0x1';
		}
	}
	public function setGenreMovie($idMovie, $genre){
		$query = "";
		foreach($genre as $value){
			$query .= "INSERT INTO genreMovie (idMovie, idGenre) VALUES ($idMovie, $value); ";
		}
		$this->closeConexion();
		$this->openConexion();
		if($this->mysqli->multi_query($query)){
			return '0x0';
		}else{
			echo $this->mysqli->error;
			return '0x1';
		}
	}
}
?>